#-*- coding:utf-8 -*-

import sys, os, commands, argparse
##########################################
# 	canshu zhiding
parser = argparse.ArgumentParser()
parser.add_argument("server_id",help="remote minion servers to operate")
parser.add_argument("cmd", choices=['fabu','huitui',], 
			help="choose the command to exec:  fabu huitui ...")
parser.add_argument("project_name", help="congfuwuqi banbenku ming")
parser.add_argument("--reposity", help="the reposity to be pulled from",
			default="origin")
parser.add_argument("--refspec", help= "src:dst ",default="master:master")

parser.add_argument("--dir_slave", help="git cangku congfuwuqi suozai mulu ",
                     default="/root/workspace/")

args = parser.parse_args()
#############################################3


##################################################
#          mingling panduan
if args.cmd == 'fabu':
    cmd_dir = "cd {0}{1}".format(args.dir_slave,args.project_name)	
    cmd = "{0} ;git pull {1} {2};git reset --hard HEAD".format(cmd_dir,args.reposity,args.refspec)
    servers = args.server_id
elif args.cmd == 'huitui':
    cmd_dir = "cd {0}".format(args.dir_slave)
    cmd = "{0} ; git reset --hard HEAD^".format(cmd_dir)
#####################################################3
# mingling zhixing 
(exitstatus,outtext) = commands.getstatusoutput("salt -L %s cmd.run '%s' "% (servers,cmd))
print outtext


#########################################
